package com.tau.iplayer.client.dialogs;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.tau.iplayer.client.IPlayer;
import com.tau.iplayer.client.IPlayerUtils;
import com.tau.iplayer.client.models.Playlist;
import com.tau.iplayer.client.models.Track;

public class RenameTrackDialog extends DialogBox {
	
	private static final boolean IS_AUTO_HIDE = false;
	private static final boolean IS_MODAL = true;
	private static final boolean IS_ANIMATED = true;
	private static final boolean IS_GLASS = true;
	private static final String HEADER = "Rename Track";
	private IPlayer iPlayer;
	private TextBox titleTextBox;
	private Track track;
	private TextBox artistTextBox;

	public RenameTrackDialog(Track track, IPlayer iplayer) {
		this(track);
		this.iPlayer = iplayer;
	}

	public RenameTrackDialog(Track track) {
		setAutoHideEnabled(IS_AUTO_HIDE);
		setModal(IS_MODAL);
		setAnimationEnabled(IS_ANIMATED);
		setGlassEnabled(IS_GLASS);
		setText(HEADER);
		this.track = track;
		createContents();
	}

	private void createContents() {
		
		HorizontalPanel horizontalPanel = new HorizontalPanel();
		final Button renameSongButton = new Button("OK");
		renameSongButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				handleRenameTrackClicked();
			}
		});
		renameSongButton.setStyleName("button");
		
		Button cancelButton = new Button("Cancel");
		cancelButton.setStyleName("button");
		cancelButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				handleCancelClick();
			}
		});
		horizontalPanel.add(renameSongButton);
		horizontalPanel.add(cancelButton);
		horizontalPanel.setSpacing(3);
		
		Grid formGrid = new Grid(2, 2);
		formGrid.setText(0, 0, "Title:");
		formGrid.getCellFormatter().setStyleName(0, 0, "standardLabel");
		titleTextBox = new TextBox();
		titleTextBox.setText(track.getTitle());
		titleTextBox.setStyleName("addSongDialogTextBox");
		titleTextBox.addKeyUpHandler(new KeyUpHandler() {
			
			@Override
			public void onKeyUp(KeyUpEvent event) {
				boolean isEnabled = !IPlayerUtils.isEmpty(titleTextBox.getValue());
				renameSongButton.setEnabled(isEnabled);
			}
		});
		formGrid.setWidget(0, 1, titleTextBox);

		formGrid.setText(1, 0, "Artist:");
		formGrid.getCellFormatter().setStyleName(1, 0, "standardLabel");
		artistTextBox = new TextBox();
		artistTextBox.setText(track.getArtist());
		artistTextBox.setStyleName("addSongDialogTextBox");
		formGrid.setWidget(1, 1, artistTextBox);
		
		VerticalPanel panel = new VerticalPanel();
		panel.add(formGrid);
		panel.add(horizontalPanel);
		panel.setCellHorizontalAlignment(horizontalPanel, HasHorizontalAlignment.ALIGN_CENTER);
		setWidget(panel);
	}

	private void handleRenameTrackClicked() {
		track.setTitle(titleTextBox.getText());
		track.setArtist(artistTextBox.getText());
		track.updateTrack(iPlayer.getViewer(), new AsyncCallback<Void>() {
			
			@Override
			public void onSuccess(Void result) {
				Playlist playlist = iPlayer.getCurrentPlaylist();
				playlist.loadTracks(iPlayer);
				RenameTrackDialog.this.hide();
			}
			
			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}
		});
	}

	private void handleCancelClick() {
		this.hide();
	}

}
